package com.expert.conrtoller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.expert.annotations.RoleAnnotation;
import com.expert.dto.EditRoleDTO;
import com.expert.dto.UpdateRoleMenuDTO;
import com.expert.enums.RoleLevel;
import com.expert.pojo.Role;
import com.expert.result.Result;
import com.expert.service.RoleService;
import com.expert.vo.AssignMenuVo;
import com.expert.vo.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: RoleController
 * @Author: cxs
 * @Date: 2024/02/06 21:01
 * @Description:
 **/
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Autowired
    RoleService roleService;

    @RoleAnnotation(level = RoleLevel.ADMIN)
    @GetMapping("/getRoleList")
    public Result<List<Role>> getRoleList() {
        log.info("获取role列表");
        List<Role> roleList = this.roleService.getRoleList();
        return Result.success(roleList);
    }

    /**
     * 分页查询角色
     *
     * @param page     当前页
     * @param pageSize 每页个数
     * @param roleName 角色名
     * @return 权限为:超级管理员
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @GetMapping("/page")
    public Result<Page<RoleVo>> pageResult(int page, int pageSize, String roleName,String roleCode) {
        log.info("分页查询角色");
        Page<RoleVo> roleVoPage = this.roleService.getRolePage(page, pageSize, roleName,roleCode);
        return Result.success(roleVoPage);
    }

    /**
     * 删除角色信息
     *
     * @param ids 待删除角色id
     * @return Result
     * 权限为:超级管理员
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @DeleteMapping("/delete")
    public Result deleteRole(@RequestParam ArrayList<Long> ids) {
        log.info("删除角色信息{}", ids);
        this.roleService.deleteRoles(ids);
        return Result.success();
    }

    /**
     * 检查新增角色信息是否重复
     *
     * @param roleCode 新增角色代号
     * @return
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @GetMapping("/check")
    public Result checkRoleRepeat(@RequestParam String roleCode) {
        this.roleService.checkRoleRepeat(roleCode);
        return Result.success();
    }

    /**
     * 检查新增角色信息是否重复
     *
     * @param roleName 新增角色代号
     * @return
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @GetMapping("/checkRoleName")
    public Result checkRoleNameRepeat(@RequestParam String roleName) {
        boolean isRepeat = this.roleService.checkRoleNameRepeat(roleName);
        return isRepeat ? Result.success("yes") : Result.success();
    }

    /**
     * 添加新角色
     *
     * @param role 待添加角色
     * @return
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @PostMapping("/add")
    public Result addRole(@RequestBody Role role) {
        log.info("添加新角色：{}", role);
        this.roleService.addRole(role);
        return Result.success();
    }

    /**
     * 根据id查询角色
     *
     * @param id 角色id
     * @return
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @GetMapping("/{id}")
    public Result<Role> getRoleById(@PathVariable("id") Long id) {
        log.info("通过id查询角色信息:{}", id);
        Role role = this.roleService.getRoleById(id);
       return Result.success(role);
    }

    /**
     * 修改用户信息
     * @param editRoleDTO 待修改的内容
     * @return
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @PutMapping("/update")
    public Result updateRole(@RequestBody EditRoleDTO editRoleDTO){
        log.info("修改角色信息:{}",editRoleDTO);
        this.roleService.updateRole(editRoleDTO);
        return Result.success();
    }
    /**
     * 查询菜单列表
     * @return
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @GetMapping("/menu")
    public Result<List<AssignMenuVo>> getAssignMenu(){
        log.info("修改角色信息:");
        List<AssignMenuVo> menuList=this.roleService.getAssignMenu();
        return Result.success(menuList);
    }

    /**
     * 查询用户默认菜单列表
     * @return
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @GetMapping("/menu/{id}")
    public Result<List<Long>> getAssignMenuIds(@PathVariable("id") Long id){
        log.info("修改角色信息:");
        List<Long> ids=this.roleService.getRoleDefaultIds(id);
        return Result.success(ids);
    }
    /**
     * 修改角色菜单列表
     * @return
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @PostMapping("/updateMenu")
    public Result updateRoleMenu(@RequestBody UpdateRoleMenuDTO menuDTO){
        log.info("修改角色信息:");
        this.roleService.updateRoleMenu(menuDTO);
       return Result.success();
    }
}
